package com.dragon.app.controller.business;

import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.logical.service.contract.ICommentService;
import com.dragon.repository.domain.dto.CommentDTO;
import com.dragon.repository.domain.vo.CommentVO;
import com.dragon.repository.entity.Comment;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 评论表 前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-07-24
 */
@RestController
@RequestMapping("comment")
@Api(tags = "评论")
public class CommentController {

    @Autowired
    ICommentService service;


    @GetMapping("commentList")
    @ApiOperation(value = "评论列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "correlationId", value = "主表id", required = true, type = "int"),
            @ApiImplicitParam(name = "streamType", value = "流程类型", required = true, type = "int")
    })
    public ResponseData<List<CommentDTO>> list(
            @RequestParam Integer correlationId,
            @RequestParam Integer streamType){
        List<Comment> comments = service.listComment(correlationId,streamType);
        return ResponseData.success(CommentDTO.convertList(comments));
    }

    @PostMapping("add")
    @ApiOperation("新增一条评论")
    public ResponseData addComment(@RequestBody @Validated(Insert.class) CommentVO vo, HttpServletRequest request){
        service.addComment(CommentVO.convert(vo),request);
        return ResponseData.success();
    }

    @PostMapping("reply")
    @ApiOperation("回复评论")
    public ResponseData replyComment(@RequestBody @Validated(Insert.class) CommentVO vo,
                                     HttpServletRequest request){
        service.replyComment(CommentVO.convert(vo),request);
        return ResponseData.success();
    }

    @PostMapping("delete")
    @ApiOperation("删除评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "评论的id" , required = true, type = "int"),
            @ApiImplicitParam(name = "streamType", value = "流程类型" , required = true, type = "int")
    })
    public ResponseData deleteComment(@RequestParam Integer id,
                                      @RequestParam Integer streamType,
                                      HttpServletRequest request){
        service.deleteComment(id, streamType, request);
        return ResponseData.success();
    }

}
